#include <stdlib.h>
#include "Arr_Stack.h"

 extern Arr_Stack *InitArrStack(int size)
 {
     Arr_Stack *newStack = (Arr_Stack *)malloc(sizeof(Arr_Stack));
     newStack->size = size;
     newStack->curLength = 0;
     newStack->headIdx = -1;
     newStack->basicArr = (ArrStackNode *)malloc(sizeof(ArrStackNode) * size);
 }
 
 extern void Push_AS(Arr_Stack* stack, ElementType newElem)
 {
     if (stack == NULL || stack->curLength >= stack->size)
     {
         return;
     }
     ArrStackNode newNode;
     newNode.element = newElem;
     stack->basicArr[stack->curLength] = newNode;
     stack->headIdx = stack->curLength;
     stack->curLength = stack->curLength + 1;
 }
 
 extern ElementType Pop_AS(Arr_Stack *stack)
 {
    if(stack == NULL || stack->curLength <= 0)
    {
        return NULL;
    }
    ElementType popElem = stack->basicArr[stack->headIdx].element;
    stack->headIdx = stack->headIdx - 1;
    stack->curLength = stack->curLength - 1;
    return popElem;
 }
 
 extern ElementType Top_AS(Arr_Stack *stack)
 {
    if(stack == NULL || stack->curLength <= 0)
    {
        return NULL;
    }
    ElementType popElem = stack->basicArr[stack->headIdx].element;
    return popElem;
 }
 
 extern void Clear(Arr_Stack *stack)
 {
    stack->curLength = 0;
    stack->headIdx = -1;
 }